package shiro;

import util.JwtToken;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import util.JwtUtil;
import util.ParameterUtils;


/**
 * 自定义ShiroRealm
 * 2019-4-3
 */
public class UserRealm extends AuthorizingRealm {

    private static Logger logger = LoggerFactory.getLogger(UserRealm.class);

    public UserRealm() {
        super();
    }

    /**
     * 认证方法
     * 只有当需要检测用户权限的时候才会调用此方法，例如checkRole,checkPermission之类的
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) {

        logger.debug("-----------进入了doGetAuthenticationInfo 认证方法-----------");

        String token = (String) authenticationToken.getCredentials();

        return new SimpleAuthenticationInfo(token, token, this.getClass().getSimpleName());

    }


    /**
     * 授权方法
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        logger.debug("-----------进入了doGetAuthorizationInfo 授权方法-----------");

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        Object principal = principalCollection.getPrimaryPrincipal();//token

        String groud = JwtUtil.tokenCentos(principal.toString(), "Groud");
        String store = JwtUtil.tokenCentos(principal.toString(), "Store");

        //运营管理员
        if (groud.equals(ParameterUtils.USER_TYPE_1)) {
            info.addRole(groud);
            info.addRole(ParameterUtils.USER_TYPE_2);
            info.addRole(ParameterUtils.USER_TYPE_3);
            info.addRole(ParameterUtils.USER_TYPE_4);
            info.addRole(ParameterUtils.USER_TYPE_5);
        }
        if (groud.equals(ParameterUtils.USER_TYPE_2)) {
            info.addRole(groud);
            info.addRole(ParameterUtils.USER_TYPE_3);
            info.addRole(ParameterUtils.USER_TYPE_4);
            info.addRole(ParameterUtils.USER_TYPE_5);
        }
        if (groud.equals(ParameterUtils.USER_TYPE_3)) {
            info.addRole(groud);
            info.addRole(ParameterUtils.USER_TYPE_4);
            info.addRole(ParameterUtils.USER_TYPE_5);
        }
        if (groud.equals(ParameterUtils.USER_TYPE_4)) {
            info.addRole(groud);
            info.addRole(ParameterUtils.USER_TYPE_5);
        } else {
            info.addRole(groud);
        }


        //卖家
        if (store.equals(ParameterUtils.NAV_TYPE_2)) {
            info.addRole(store);
            info.addRole(ParameterUtils.NAV_TYPE_1);
        } else {
            //买家用户
            info.addRole(store);
        }

        return info;
    }

    /**
     * 必须重写此方法，不然Shiro会报错
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

}
